﻿
using UnityEngine;
using System.IO;
using UnityEditor;


/**
 *   版权：博毅创为教育咨询有限公司  2018
 *   作者：Felix老师
 */


public class AStarMapUitl {
    public static void CreatCSharpMapFile(GameObject selectObj, string writePath)
    {
        string fileName = selectObj.name;
        string className = fileName;
        StreamWriter sw = new StreamWriter(Application.dataPath + writePath + className + ".cs");

        TileCreator map = selectObj.GetComponent<TileCreator>();
        map.ReadMap();

        float Size = map.Size;
        float Width = map.Width;
        float Height = map.Height;
        bool[,] GoMap = map.GoMap;

        sw.WriteLine("using UnityEngine;\nusing System.Collections;\n");
        sw.WriteLine("public class " + className + "_MapData {");
        sw.WriteLine("\tstatic public float Size = " + Size + ";");
        sw.WriteLine("\tstatic public int Width = " + Width + ";");
        sw.WriteLine("\tstatic public int Height = " + Height + ";\n");
        sw.WriteLine("\tstatic public bool[,] GoMap = new bool[,]{");

        for (int i = 0; i < Width; i++) {
            string lineString = "\t\t{ ";
            for (int j = 0; j < Height; j++) {
                if (GoMap[i, j]) {
                    lineString += ("true, ");
                }
                else {
                    lineString += ("false, ");
                }
                
            }

            lineString += " }, ";
            sw.WriteLine(lineString);
        }

        sw.WriteLine("\t};");

        sw.WriteLine("}");

        
        sw.Flush();
        sw.Close();
        AssetDatabase.Refresh();        //这里是一个点
    }

    public static void CreatTSMapFile(GameObject selectObj, string writePath)
    {
        string fileName = selectObj.name;
        string className = fileName;
        StreamWriter sw = new StreamWriter(Application.dataPath + writePath + className + ".ts");

        TileCreator map = selectObj.GetComponent<TileCreator>();
        map.ReadMap();

        float Size = map.Size;
        float Width = map.Width;
        float Height = map.Height;
        bool[,] GoMap = map.GoMap;

        // sw.WriteLine("using UnityEngine;\nusing System.Collections;\n");
        sw.WriteLine("export default class " + className + "_MapData {");
        sw.WriteLine("\tpublic static Size = " + Size + ";");
        sw.WriteLine("\tpublic static Width = " + Width + ";");
        sw.WriteLine("\tpublic static Height = " + Height + ";\n");
        sw.WriteLine("\tpublic static GoMap = [");

        for (int j = 0; j < Height; j++) 
        {
            string lineString = "\t\t[ ";
            for (int i = 0; i < Width; i++)
            {
                if (GoMap[i, j])
                {
                    lineString += ("true, ");
                }
                else
                {
                    lineString += ("false, ");
                }

            }

            lineString += " ], ";
            sw.WriteLine(lineString);
        }

        sw.WriteLine("\t];");

        sw.WriteLine("}");


        sw.Flush();
        sw.Close();
        AssetDatabase.Refresh();        //这里是一个点
    }
}
